import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { ActivatedRoute } from "@angular/router";
import { Storage } from '@ionic/storage';
import { ScreenOrientation } from '@ionic-native/screen-orientation/ngx';
import {
  SessionServiceProxy,
  OrganizationUnitExtensionServiceProxy,
} from '../../shared/service-proxies/service-proxies';
import { AppConsts } from '../../shared/AppConsts';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
  userInfo: any;
  userName: any;
  idParam: number;
  repairId: number;//id
  repairUnit: string;//部门
  repairUnitId: number;//部门id
  constructor(
    public router: Router,
    private storage: Storage,
    public routeData: ActivatedRoute,
    private screenOrientation: ScreenOrientation,
    private _SessionServiceProxy: SessionServiceProxy,
    private _OrganizationUnitExtensionServiceProxy: OrganizationUnitExtensionServiceProxy,
  ) {
    this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT);
  }

  ionViewDidEnter() {
    this.checkLogin();
  }


  itemData = [
    { title: "日供水量", page: "daily-report", label: "查看当月每日供水情况" },
    { title: "月供水量", page: "month-report", label: "查看年度每月供水情况" },
    { title: "月供水分布", page: "month-distribution-report", label: "查看各厂月供水占比情况" },
    { title: "供水量信息表", page: "month-water-report", label: "查看当月平均日供水量" },
    { title: "生产成本", page: "cost", label: "查看每月公司单位生产成变动情况" },
    { title: "总产销差", page: "sell", label: "查看每月产销差百分比" },
    { title: "SCADA工单", page: "scada", label: "未处理的SCADA工单" }
  ];

  async checkLogin() {
    let h = await this.storage.get('accessToken');
    //  console.log(".............", h);
    if (!!h) {
      AppConsts.sessonToken = h;
    } else {
      this.router.navigate(['login'])
    }
    this.getUserInfo();
    // this.getUnitsByUser();
  }

  getUserInfo() {
    this._SessionServiceProxy.getCurrentLoginInformations()
      .subscribe((res) => {
        // console.log("用户信息", res);
        this.userInfo = res;
        if (this.userInfo.user == undefined) {
          this.router.navigate(['login'])
        } else {
          this.userName = this.userInfo.user.surname + this.userInfo.user.name;
          this.repairId = this.userInfo.user.id;
          console.log("user", this.repairId);

          //根据用户id获取部门----报修部门
          this._OrganizationUnitExtensionServiceProxy.getOrganizationUnitByUser(this.idParam, false).subscribe((res) => {
            this.repairUnit = res[0].displayName;
            this.repairUnitId = res[0].id;
            console.log("部门", res)
          })
        }

      })
  }

  //根据用户id获取部门----报修部门
  // getUnitsByUser() {
  //   console.log("456436432")
  //   this.idParam = this.repairId;
  //   if (!!this.idParam) {
  //     this._OrganizationUnitExtensionServiceProxy.getOrganizationUnitByUser(this.idParam, false).subscribe((res) => {
  //       this.repairUnit = res[0].displayName;
  //       this.repairUnitId = res[0].id;
  //       console.log("部门", res)
  //     })
  //   }
  // }

  selectedItem: any;
  selectedPage: string;
  onSelectedData(item) {
    this.selectedItem = item;
    this.selectedPage = item.page;
    this.router.navigateByUrl(this.selectedPage)
  }

  go() {
    this.router.navigateByUrl('/login');
  }
}
